package com.holi.hiteacher.activity;

import android.databinding.DataBindingUtil;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.text.TextUtils;
import android.util.Log;
import android.view.View;
import android.widget.EditText;
import android.widget.Toast;

import com.holi.hiteacher.R;
import com.holi.hiteacher.databinding.ActivityTestBinding;
import com.holi.hiteacher.net.MainViewModel;
import com.holi.hiteacher.net.UserModel;
import com.holi.hiteacher.utils.Util;

import java.util.LinkedHashSet;
import java.util.Set;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;
import cn.jpush.android.api.JPushInterface;
import cn.jpush.android.api.TagAliasCallback;

public class TestActivity extends BaseActivity {
    @BindView(R.id.edtTag)
    EditText edtTag;
    @BindView(R.id.edtAlias)
    EditText edtAlias;

    private static final int MSG_SET_ALIAS = 1001;
    private static final int MSG_SET_TAGS = 1002;

    private ActivityTestBinding binding;
    private MainViewModel viewModel;
    private UserModel model;

    private Handler mHandler = new Handler() {
        @Override
        public void handleMessage(Message msg) {
            super.handleMessage(msg);
            switch (msg.what) {
                case MSG_SET_ALIAS:
                    JPushInterface.setAlias(getApplicationContext(), (String) msg.obj, mTagsCallback);
                    break;
                case MSG_SET_TAGS:
                    JPushInterface.setTags(getApplicationContext(), (Set<String>) msg.obj, mAliasCallback);
                    break;
                default:
                    Log.i(TAG, "Unhandled msg - " + msg.what);
            }
        }
    };

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_test);
        binding = DataBindingUtil.setContentView(this, R.layout.activity_test);
        ButterKnife.bind(this);
        viewModel = new MainViewModel();
        binding.setModel(viewModel);
        model = new UserModel(viewModel);

        binding.button.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                viewModel.setPb(true);
                model.getUser(binding.username.getText().toString());
            }
        });
    }

    @OnClick({R.id.btnTag, R.id.btnAlias})
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.btnTag:
                setTag();
                break;
            case R.id.btnAlias:
                setAlias();
                break;
        }
    }

    private void setTag() {
        String tag = edtTag.getText().toString().trim();

        // 检查 tag 有效性
        if (Util.isEmpty(tag)) {
            Toast.makeText(this, "tag不能为空", Toast.LENGTH_SHORT).show();
            return;
        }

        // “,”隔开多个 转换成 Set
        String[] sArray = tag.split(",");
        final Set<String> tagSet = new LinkedHashSet<>();
        for (String sTagItem : sArray) {
            if (!Util.isValidTagAndAlias(sTagItem)) {
                Toast.makeText(this, "格式不对", Toast.LENGTH_SHORT).show();
                return;
            }
            tagSet.add(sTagItem);
        }

        // 调用 JPush API 设置Tag
        mHandler.sendMessage(mHandler.obtainMessage(MSG_SET_TAGS, tagSet));
    }

    private void setAlias() {
        String alias = edtAlias.getText().toString().trim();
        if (TextUtils.isEmpty(alias)) {
            Toast.makeText(this, "不能为空", Toast.LENGTH_SHORT).show();
            return;
        }
        if (!Util.isValidTagAndAlias(alias)) {
            Toast.makeText(this, "格式不对", Toast.LENGTH_SHORT).show();
            return;
        }

        //调用JPush API设置Alias
        mHandler.sendMessage(mHandler.obtainMessage(MSG_SET_ALIAS, alias));
    }

    private final TagAliasCallback mTagsCallback = new TagAliasCallback() {
        @Override
        public void gotResult(int code, String s, Set<String> tags) {
            String logs;
            switch (code) {
                case 0:
                    logs = "Set tag success";
                    break;
                case 6002:
                    logs = "Failed to set alias and tags due to timeout. Try again after 60s.";
                    Log.w(TAG, logs);
                    if (Util.isConnected(getApplicationContext()))
                        mHandler.sendMessageDelayed(mHandler.obtainMessage(MSG_SET_TAGS,
                                tags), 1000 * 60);
                    else
                        Log.w(TAG, "No network");
                    break;
                default:
                    logs = "Failed with errorCode = " + code;
                    Log.w(TAG, logs);
            }
        }
    };

    private final TagAliasCallback mAliasCallback = new TagAliasCallback() {
        @Override
        public void gotResult(int code, String s, Set<String> set) {
            String logs;
            switch (code) {
                case 0:
                    logs = "Set alias failed!";
                    Log.w("joshua", logs);
                    break;
                case 6002:
                    logs = "Failed to set alias and tags due to timeout. Try again after 60s.";
                    Log.w("joshua", logs);
                    if (Util.isConnected(getApplicationContext())) {
                        mHandler.sendMessageDelayed(mHandler.obtainMessage(MSG_SET_ALIAS),
                                1000 * 60);
                    }
                    break;
            }
        }
    };

}
